<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/12/12
 * Time: 10:15
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class News  extends Base {
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['a.new_title'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($province)&&$province!="")
            {
                $map['a.new_status'] = $province;
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.new_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.new_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.new_time'] = ['between time',[$start,$end]];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('new')->alias('a')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('new')->alias('a')
                ->where($map)
                ->field('a.*')
                ->page($NowPage,$limits)
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }


    /***
     * @return mixed|\think\response\Json
     * 添加
     */
    public function add(){
        if(Request::instance()->isPost()){
            $new_title =input('param.new_title');
            $new_second_title =input('param.new_second_title');
            $new_img = input('param.new_img');
            $new_type = input('param.new_type');
            $new_link = input('param.new_link');
            $new_content = input('param.new_content');
            $data = [
                'new_title' => $new_title,
                'new_second_title' => $new_second_title,
                'new_img' => $new_img,
                'new_time' => date('Y-m-d H:i:s'),
                'new_type' => $new_type,
                'new_link' => $new_link,
                'new_content'=>$new_content,
            ];
            $res = Db::table('up_new')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    /***
     * @return mixed
     * 编辑
     */
    public function update(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $new_title =input('param.new_title');
            $new_second_title =input('param.new_second_title');
            $new_type = input('param.new_type');
            $new_link = input('param.new_link');
            $new_content = input('param.new_content');
            $data['new_title'] = $new_title;
            $data['new_second_title'] = $new_second_title;
            $data['new_type'] = $new_type;
            $data['new_link'] = $new_link;
            $data['new_content'] = $new_content;
            if(!empty(input('new_img'))){
                $data['new_img'] = input('new_img');
            }
            $res = Db::table('up_new')->where('new_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $notice_id = input('param.notice_id');
        $res = Db::table('up_new')->where('new_id',$notice_id)->find();
        $this->assign('id',$notice_id);
        $this->assign('res',$res);
        return $this->fetch();
    }



    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $task_id = input('param.id');
        $res = Db::name('new')->where('new_id',$task_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
    /***
     * @return \think\response\Json
     * 上架
     */
    public function up(){
        $task_id = input('param.id');
        $res = Db::name('new')->where('new_id',$task_id)->setField('new_status',1);
        if($res){
            return json(['code'=>200,'msg'=>'上架成功']);
        }else{
            return json(['code'=>100,'msg'=>'上架失败']);
        }
    }
    /***
     * @return \think\response\Json
     * 下架
     */
    public function down(){
        $task_id = input('param.id');
        $res = Db::name('new')->where('new_id',$task_id)->setField('new_status',2);
        if($res){
            return json(['code'=>200,'msg'=>'下架成功']);
        }else{
            return json(['code'=>100,'msg'=>'下架失败']);
        }
    }
}